I've been searching for a way to do the equivalent of zipping and unzipping arrays in Javascript. I've found a way to to the zipping, but not the unzipping.
Overview:
I have two lists and the first members of the lists go together, the second members go together, etc. I would like to shuffle the lists to retain this pairing, and then end up with two separate lists once again.
In Python:
#Define shuffle function
shuffle = util.shuffle;
#Lists of adult and child animals
adult = ["cat", "dog", "chicken"]
child = ["kitty", "puppy", "chick"]
#zip them so I can shuffle while keeping pairs intact; then unzip them
animals = list(zip(adult, child))
random.shuffle(animals)
adult, child = zip(*animals)
adult = list(adult)
child = list(child)
In Javascript:
In the PsychoPy forums I found a way to do the zipping that will work in Pavlovia.
animals = []
for Idx in range(len(adult)):
animals.append([adult[Idx],labelsWhole[Idx%len(child)]])
shuffle(animals)
Outstanding:
But now how can I do the equivalent of: adult, child = zip(*animals)?
I am aware of this similar post. However, there is only one comment mentioning "unzipping" and it won't work in PsychoPy.
You can iterate over the zipped list and use tuple unpacking
for(const [adult,child] of zippedList)
....
I created a sandbox demo here